﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain;
using Service.Interface;
using Clannan.Helper;

namespace Clannan.Controllers
{
    public class ManageController : Controller
    {
        public IUserInfoService UserInfoService;

        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 后台登陆
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="validateCode">验证码</param>
        /// <returns></returns>
        public JsonResult LoginIsPass(string name, string password, string validateCode)
        {
            //验证
            if (validateCode != (Session["ValidateCode"] ?? "").ToString())
            {
                return Json(new { Message = 0, Error = "验证码错误" });
            }
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                return Json(new { Message = 0, Error = "用户名或密码不能为空" });
            }

            UserInfo userinfo = this.UserInfoService.Get(name, password);
            if (userinfo != null && (userinfo.Role.Name == "管理员" || userinfo.Role.Name == "超级管理员"))
            {
                //添加Cookit到客户端
                AddCookit addCookit = new AddCookit();
                addCookit.AddCookitToLoginUser(userinfo.ID.ToString(), userinfo.Role.Name, 1);

                return Json(new { Message = 1 });
            }
            else
            {
                return Json(new { Message = 0, Error = "用户名或密码错误，登陆失败！" });
            }
        }
    }
}
